package com.lanqiao.gxnushop.mapper;

import com.lanqiao.gxnushop.entity.Orders;
import com.lanqiao.gxnushop.entity.Products;
import com.lanqiao.gxnushop.valueObject.OrderDetails;
import io.swagger.v3.oas.annotations.media.Schema;
import org.apache.ibatis.annotations.*;

import java.util.ArrayList;

@Mapper
@Schema(description ="订单表")
public interface OrdersMapper {
    @Schema(description ="添加订单")
    @Insert("insert into orders(orderId,storeId,productId,buyerId,addressId,quantity,totalAmount,state,zcsj) values (#{orderId},#{storeId},#{productId},#{buyerId},#{addressId},#{quantity},#{totalAmount},#{state},now())")
    boolean add(Orders orders);

    @Schema(description ="删除订单")
    @Delete("delete from orders where orderId=#{orderId}")
    boolean delete(String ordersId);

    @Schema(description ="根据订单id查询订单")
    @Select("select * from orders where orderId=#{orderId}")
    Orders findById(String ordersId);

    /*@Schema(description ="根据买家id查询所有订单")
    @Select("select * from orders where buyerId=#{buyerId}")
    ArrayList<Orders> findByBuyerId(String buyerId);*/

    @Schema(description ="根据店铺id查询订单")
    @Select("select * from orders where storeId=#{storeId}")
    ArrayList<Orders> findByStoreId(String storeId);

    @Schema(description ="更新订单")
    @Update("update orders set quantity=#{quantity},totalAmount=#{totalAmount},state=#{state} where orderId=#{orderId}")
    boolean update(Orders orders);

    @Schema(description ="根据买家id查询所有订单，并获取商品名称和店铺名称")
    @Select("SELECT o.orderId, o.storeId, o.productId, o.buyerId, o.addressId, o.zcsj, o.state, o.quantity, o.totalAmount, " +
            "p.name AS productName, s.name AS storeName " +
            "FROM orders o " +
            "JOIN products p ON o.productId = p.productId " +
            "JOIN stores s ON o.storeId = s.storeId " +
            "WHERE o.buyerId = #{buyerId}")
    ArrayList<OrderDetails> findByBuyerId(String buyerId);


    /*
    @Schema(description ="根据订单id查询店铺")
    @Select("select * from stores where storeId=(select storeId from orders where ordersId=#{ordersId})")
    Products findStoreByOrdersId(String ordersId);

    @Schema(description ="根据订单id查询买家")
    @Select("select * from buyers where buyerId=(select buyerId from orders where ordersId=#{ordersId})")
    Products findBuyerByOrdersId(String ordersId);*/
}
